//
//  YJLoginView.m
//  YJB
//
//  Created by 中电大维 on 16/5/12.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJLoginView.h"
#import "YJNetworkTool.h"
#import "YJUserDefaultTool.h"
#import "Masonry.h"
#import "YJGlobalHeader.h"
#import "YJLoginViewModel.h"

@interface YJLoginView ()
@property (strong, nonatomic)UITextField *userName;
@property (strong, nonatomic)UITextField *passWord;
@property(nonatomic,strong)UIButton *forgetBtn;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UIButton *registBtn;

@property(nonatomic,strong)YJLoginViewModel  *loginViewModel;

@end

@implementation YJLoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.forgetBtn.contentMode = UIViewContentModeRight;
        @weakify(self)
        [[NOTIFICATIONCENTER rac_addObserverForName:YJUserDidSuccessRegistNotification object:nil] subscribeNext:^(NSNotification *notifi) {
            @strongify(self)
            NSDictionary *userInfo = notifi.userInfo;
            self.userName.text = userInfo[@"userName"];
            self.passWord.text = userInfo[@"pasword"];
            self.loginViewModel.username = self.userName.text;
            self.loginViewModel.pwd = self.passWord.text;
            self.loginBtn.enabled = YES;
        }];
        //用户修改了密码
        [[[NOTIFICATIONCENTER rac_addObserverForName:YJUserDidChangePassWordNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
            [YJKeychainTool savePassword:@""];
            self.loginViewModel.pwd = nil;
            self.passWord.text = nil;
            self.loginBtn.enabled = NO;
        }];

        //获取缓存的账号及密码
        self.userName.text = [YJUserDefaultTool readUserAccount];
        self.passWord.text = [YJKeychainTool readPassword];
        //绑定
        RAC(self.loginViewModel,username) = self.userName.rac_textSignal;
        RAC(self.loginViewModel,pwd) = self.passWord.rac_textSignal;
        //登录按钮的交互性
        RAC(self.loginBtn,enabled) = self.loginViewModel.loginBtnEnableSignal;
        //按钮事件
        [[self.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self endEditing:YES];
            [self.loginViewModel.loginCommand execute:nil];
        }];
        
        self.registSignal = [RACSubject subject];
        self.forgetSignal = [RACSubject subject];
        [[self.registBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self.registSignal sendNext:x];
        }];
        [[self.forgetBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self.forgetSignal sendNext:x];
        }];
    }
    return self;
}

-(YJLoginViewModel *)loginViewModel
{
    if (_loginViewModel == nil)
    {
        _loginViewModel = [[YJLoginViewModel alloc]init];
    }
    return _loginViewModel;
}

- (UITextField *)userName
{
    if (_userName == nil)
    {
         _userName = [[UITextField alloc]init];
         _userName.clearButtonMode = UITextFieldViewModeAlways;
        [_userName setBackground:Image(@"login_inputView_bg")];
        _userName.placeholder = @"用户名/手机号";
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 1)];
        _userName.leftView = leftView;
        _userName.leftViewMode = UITextFieldViewModeAlways;
        _userName.font = FONT(16);
        [self addSubview:_userName];
        [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(17.5);
            make.right.mas_equalTo(-17.5);
            make.height.mas_equalTo(44);
            make.top.mas_equalTo(15 * GRIDH);
        }];
    }
    return _userName;
}

- (UITextField *)passWord
{
    if (_passWord == nil)
    {
         _passWord = [[UITextField alloc]init];
        [_passWord setBackground:Image(@"login_inputView_bg")];
        _passWord.secureTextEntry = YES;
        _passWord.clearButtonMode = UITextFieldViewModeAlways;
        UIView *passWordleftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 1)];
        _passWord.leftView = passWordleftView;
        _passWord.leftViewMode = UITextFieldViewModeAlways;
        _passWord.placeholder = @"密码";
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 1)];
        _passWord.leftView = leftView;
        _passWord.leftViewMode = UITextFieldViewModeAlways;
        _passWord.font = FONT(16);
        [self addSubview:_passWord];
        [_passWord mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(17.5);
            make.right.mas_equalTo(-17.5);
            make.height.mas_equalTo(44);
            make.top.mas_equalTo(self.userName.mas_bottom).offset(12);
        }];
    }
    return _passWord;
}

- (UIButton *)loginBtn
{
    if (_loginBtn == nil)
    {
        _loginBtn = [[UIButton alloc]init];
        [_loginBtn setBackgroundImage:Image(@"button_icon") forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:Image(@"button_icon_HL") forState:UIControlStateHighlighted];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:_loginBtn];
        [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(17.5);
            make.right.mas_equalTo(-17.5);
            make.height.mas_equalTo(45);
            make.top.mas_equalTo(self.passWord.mas_bottom).offset(15);
        }];
    }
    return _loginBtn;
}

- (UIButton *)registBtn
{
    if (_registBtn == nil)
    {
        UILabel *tip = [[UILabel alloc]init];
        tip.text = @"还没有账号?";
        tip.textColor = kUIColorFromRGB(0xC9C9C9);
        tip.font = FONT(14);
        [self addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.loginBtn.mas_left).offset(5);
            make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(12);
            make.width.mas_equalTo(82);
            make.height.mas_equalTo(26);
        }];
        //立即注册按钮
        _registBtn = [[UIButton alloc]init];
        _registBtn.titleLabel.font = FONT(14);
        _registBtn.contentMode = UIViewContentModeLeft;
        [_registBtn setTitle:@"立即注册" forState:UIControlStateNormal];
        [_registBtn setTitleColor:APPCOLOR forState:UIControlStateNormal];
        [self addSubview:_registBtn];
        [_registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(tip.mas_right).offset(8);
            make.top.mas_equalTo(tip.mas_top);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(26);
        }];
    }
    return _registBtn;
}

- (UIButton *)forgetBtn
{
    if (_forgetBtn == nil)
    {
        _forgetBtn = [[UIButton alloc]init];
        _forgetBtn.titleLabel.font = FONT(14);
        [_forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetBtn setTitleColor:APPCOLOR forState:UIControlStateNormal];
        _forgetBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self addSubview:_forgetBtn];
        [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.loginBtn.mas_right).offset(-5);
            make.top.mas_equalTo(self.registBtn.mas_top);
            make.width.mas_equalTo(75);
            make.height.mas_equalTo(26);
        }];
    }
    return _forgetBtn;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}


@end
